import { defineStore } from 'pinia'
import { message } from "ant-design-vue";
import { TOKEN_KEY } from "@/config/cacheKey";
import {
    get_user_info,
    logout_api
} from "@/apis";
export const useUserStore = defineStore('user', {
    state: () => ({ token: "", userInfo: undefined }),
    actions: {
        logout() {
            if (getStore(TOKEN_KEY)) {
                logout_api()
                removeStore(TOKEN_KEY);
            }
            this.token = ""
            this.userInfo = null
            let router = useRouter();
            let route = useRoute();
            if (route.path !== '/login') {
                router.push('/login');
                // message.success("已退出!");
            }
        },

        setUser() {
            this.token = getStore(TOKEN_KEY);
            if (this.token) {
                // 调用户信息接口获取用户信息
                get_user_info().then(res => {
                    this.userInfo = res.data;
                    console.log("用户信息", this.userInfo);
                })
            }
        }
    }
})